package com.zzgc.modules.service.impl;

import com.zzgc.common.dao.SchoolGradeClassDao;
import com.zzgc.common.dao.StudentDao;
import com.zzgc.common.entity.SchoolGrade;
import com.zzgc.common.entity.SchoolGradeClass;
import com.zzgc.common.entity.Student;
import com.zzgc.modules.controller.BaseController;
import com.zzgc.modules.service.SchoolGradeClassService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 学校班级(SchoolGradeClass)表服务实现类
 *
 * @author makejava
 * @since 2022-11-22 09:06:56
 */
@Service("schoolGradeClassService")
public class SchoolGradeClassServiceImpl implements SchoolGradeClassService {
    @Resource
    private SchoolGradeClassDao schoolGradeClassDao;
    @Resource
    private StudentDao studentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SchoolGradeClass queryById(Long id) {
        return this.schoolGradeClassDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param schoolGradeClass 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SchoolGradeClass> queryByPage(SchoolGradeClass schoolGradeClass) {
        List<SchoolGradeClass> gradeClasses = this.schoolGradeClassDao.queryAllByLimit(schoolGradeClass);
        long count = this.schoolGradeClassDao.count(schoolGradeClass);
        if (count == 0) {
            return new ArrayList<>();
        }
        gradeClasses.get(0).setTotal(count);
        return gradeClasses;
    }

    /**
     * 分页查询
     *
     * @param schoolGradeClass 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SchoolGradeClass> queryByPageByYear(SchoolGradeClass schoolGradeClass) {
        List<SchoolGradeClass> gradeClasses = this.schoolGradeClassDao.queryAllByLimitByYear(schoolGradeClass);
        long count = this.schoolGradeClassDao.countByYear(schoolGradeClass);
        if (count == 0) {
            return new ArrayList<>();
        }
        gradeClasses.get(0).setTotal(count);
        return gradeClasses;
    }

    /**
     * 新增数据
     *
     * @param schoolGradeClass 实例对象
     * @return 实例对象
     */
    @Override
    public SchoolGradeClass insert(SchoolGradeClass schoolGradeClass) {
        List<SchoolGradeClass> schoolGradeClasses = schoolGradeClassDao.queryAllBy(schoolGradeClass.getSSchoolId(), schoolGradeClass.getSName(), schoolGradeClass.getSGradeId());
        if (schoolGradeClasses.size() > 0){
            throw new RuntimeException("该年级已存在: " + schoolGradeClass.getSName());
        }
        this.schoolGradeClassDao.insert(schoolGradeClass);
        return schoolGradeClass;
    }

    /**
     * 修改数据
     *
     * @param schoolGradeClass 实例对象
     * @return 实例对象
     */
    @Override
    public SchoolGradeClass update(SchoolGradeClass schoolGradeClass) {
        this.schoolGradeClassDao.update(schoolGradeClass);
        return this.queryById(schoolGradeClass.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        // 查询Student表是否有学生数据
        Student student = new Student();
        student.setSClassId(id);
        student.setSize(Integer.MAX_VALUE);
        List<Student> students = studentDao.queryAllByLimit(student);
        if (students.size() > 0){ // 有学生就删除
            // 删除学生
            studentDao.deleteByIds(students);
        }
        return this.schoolGradeClassDao.deleteById(id) > 0;
    }
}
